package com.yb0os1.Iterator;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Test {
    static long pos = 0L;

    public static void main(String[] args) {
//        List<User> list = new ArrayList<>();
//        list.add(new User("yb0os1", 18));
//        list.add(new User("yb0os2", 19));
//        list.add(new User("yb0os3", 11));
//        list.add(new User("yb0os4", 11));
//        for (User user : list) {
//            if (user.getAge()== 11){
//                list.add(new User("yb0os5", 11));
//                break;
//            }
//            System.out.println(user);
//        }
//
//
//        User yb0os1 = new User("yb0os1", 18);
//        for (String s : yb0os1) {
//            System.out.println(s);
//        }


//        int batchLineSize = 3;
//        int currentLine = 0;
//
//        List<User> userList = new ArrayList<>();
//        readPart(currentLine, batchLineSize, userList);
//        System.out.println(userList);
//        readPart(currentLine, batchLineSize, userList);
//        System.out.println(userList);


        UserFile userFile = new UserFile(new File("demo.user"),10);
        for (User user : userFile) {
            System.out.println(user);
        }
    }

    private static void readPart(int currentLine, int batchLineSize, List<User> userList) {
        try (RandomAccessFile br = new RandomAccessFile("demo.user", "r")) {
            br.seek(pos);
            while (currentLine < batchLineSize) {
                String line = new String(br.readLine().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
                System.out.println(line);
                String[] split = line.substring(1, line.length() - 1).split(",");
                User user = new User(split[0], Integer.parseInt(split[1]));
                userList.add(user);
                currentLine++;
            }
            pos = br.getFilePointer();
            System.out.println(pos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
